# pylint: disable-msg=W0232
"""
@author: Sergiy Tsymbalyuk
"""

from sqlalchemy import Column, ForeignKey, Table
from sqlalchemy.orm import mapper
from sqlalchemy.types import Integer, BigInteger, String, Text, Date, Enum, Boolean
from sqlalchemy.orm import relation, backref

from pylons.i18n import lazy_ugettext as _

from hrms.model.meta import Base
from hrms.model.organization import Position
from chowa.model.abstract import Person
from chowa.model.organization import Division, Office

__all__ = ['Person', 'Employee', 'Child',
           'EmploymentHistory']

employee = Table('hrms_employee', Base.metadata,
    Column('id', Integer, ForeignKey(Person.id), primary_key=True),
    Column('iin', BigInteger, unique=True, info={'verbose': _('IIN')}),
    Column('rank', Integer, info={'verbose': _('Rank')}),
    Column('is_employee', Boolean, default=True, info={'verbose': _('Is employee')}),
    Column('is_stuffer', Boolean, default=True, info={'verbose': _('Is stuffer')}),
    Column('position_id', Integer, ForeignKey(Position.id), info={'verbose': _('Position')}),
    Column('division_id', Integer, ForeignKey(Division.id), info={'verbose': _('Division')}),
    Column('office_id', Integer, ForeignKey(Office.id), info={'verbose': _('Office')}),
    Column('internal_phone', String(20), info={'verbose': _('Internal')}),
    Column('local_phone', String(20), info={'verbose': _('Local')}),
    Column('voip_phone', String(20), info={'verbose': _('VoIP')}),
    Column('mobile_phone', String(20), info={'verbose': _('Mobile')}),
    Column('email_address', String(255), info={'verbose': _('Email address')}))

class Employee(Person):
    __table__ = employee
    __mapper_args__ = {'polymorphic_identity': 'employee',
                       'order_by': (Person.surname, Person.name, Person.patronymic)
                       }
    
    position = relation(Position, order_by=Position.name)
    division = relation(Division, backref='employees')
    office = relation(Office)
    person = relation(Person)
    

class EmployeeName(Base):
    __tablename__ = 'hrms_employee_name'
    
    id = Column(Integer, primary_key=True)
    
    surname = Column(String(100))
    name = Column(String(100))
    patronymic = Column(String(100))
    initiated = Column(Date)
    outdated = Column(Date)
    is_current = Column(Boolean)
    
    employee_id = Column(Integer, ForeignKey(Employee.id))
    employee = relation(Employee)


class Child(Base):
    __tablename__ = 'hrms_child'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(100), info={'verbose': _('Person|Name')})
    born = Column(Date, info={'verbose': _('Born')})
    gender = Column(Enum('M', 'F'), info={'verbose': _('Gender')})
    parent_id = Column(Integer, ForeignKey(Employee.id))
    
    employee = relation(Employee, backref='children')
    
    def __unicode__(self):
        return self.name
    

class EmploymentHistory(Base):
    __tablename__ = 'hrms_employment_history'
    
    id = Column(Integer, primary_key=True)
    employee_id = Column(Integer, ForeignKey(Employee.id))
    started = Column(Date, info={'verbose': _('Started')})
    ended = Column(Date, info={'verbose': _('Ended')})
    is_financial = Column(Boolean, info={'verbose': _('Is financial')})
    is_state = Column(Boolean, info={'verbose': _('Is state')})
    is_treasury = Column(Boolean, info={'verbose': _('Is treasury')})
    description = Column(Text, info={'verbose': _('Description')})
    
    employee = relation(Employee, backref='history')
    
    def __unicode__(self):
        return '%s: %s' % (unicode(self.employee), self.description)
